Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MkMaterialBadge

github Show source on GitHub

Node for a CSS-based badge a la MkDocs-Material.

Examples

Example: Regular

Note

Jinja

{{ "mdi:wrench" | MkMaterialBadge(text="text") }}

Note

Python

MkMaterialBadge('mdi:wrench', 'text')
Rendered

wrench text

Markdown
<img src="https://api.iconify.design/mdi/wrench.svg?height=16" alt="wrench"> <code>text</code>
Html
<p><img src="https://api.iconify.design/mdi/wrench.svg?height=16" alt="wrench"> <code>text</code></p>

GitHub
<img src="https://api.iconify.design/mdi/wrench.svg?height=16" alt="wrench"> <code>text</code>

Example: Heart type

Note

Jinja

{{ "mdi:file" | MkMaterialBadge(text="text", animated=True) }}

Note

Python

MkMaterialBadge('mdi:file', 'text', animated=True)
Rendered

file text

Markdown
<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>
Html
<p><img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code></p>

GitHub
<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>

Example: Right aligned

Note

Jinja

{{ "mdi:file" | MkMaterialBadge(text="text", align_right=True) }}

Note

Python

MkMaterialBadge('mdi:file', 'text', align_right=True)
Rendered

file text

Markdown
<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>
Html
<p><img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code></p>

GitHub
<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>

Note

Jinja

{{ "mdi:file" | MkMaterialBadge(text="text", target="https://phil65.github.io/mknodes") }}

Note

Python

MkMaterialBadge('mdi:file', 'text', target='https://phil65.github.io/mknodes')
Rendered

file text

Markdown
[<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>](https://phil65.github.io/mknodes)
Html
<p><a href="https://phil65.github.io/mknodes"><img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code></a></p>

GitHub
[<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>](https://phil65.github.io/mknodes)
DocStrings

class MkMaterialBadge(mktemplate.MkTemplate)
Base classes
⋔ Inheritance diagram
graph TD
  3179977919["mkmaterialbadge.MkMaterialBadge"]
  1289759389["mktemplate.MkTemplate"]
  3160803176["rendercache.TemplateRenderMixin"]
  2751680898["builtins.object"]
  835538629["mkcontainer.MkContainer"]
  1956333208["mkcontainer.MkContainerBase"]
  1359323199["mknode.MkNode"]
  1289759389 --> 3179977919
  3160803176 --> 1289759389
  2751680898 --> 3160803176
  835538629 --> 1289759389
  1956333208 --> 835538629
  1359323199 --> 1956333208
  2751680898 --> 1359323199
NodeFile

/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkmaterialbadge/metadata.toml

[metadata]
name = "MkMaterialBadge"
icon = "simple-icons:shieldsdotio"
group = "badge"

[[resources.css]]
filename = "materialbadge.css"

[examples.regular]
title = "Regular"
jinja = """
{{ "mdi:wrench" | MkMaterialBadge(text="text") }}
"""

[examples.heart_type]
title = "Heart type"
jinja = """
{{ "mdi:file" | MkMaterialBadge(text="text", animated=True) }}
"""

[examples.right_aligned]
title = "Right aligned"
jinja = """
{{ "mdi:file" | MkMaterialBadge(text="text", align_right=True) }}
"""

[examples.linked]
title = "With link"
jinja = """
{{ "mdi:file" | MkMaterialBadge(text="text", target="https://phil65.github.io/mknodes") }}
"""

[output.html]
template = """
<span class="md-typeset mdx-badge{{ " mdx-badge--right" if node.align_right else "" }}{{ " mdx-badge--heart" if node.animated else "" }}">
{% set icon = "[" ~ node.icon | get_emoji_slug ~ "](" ~ node.target | get_url ~ ")" if node.target else node.icon | get_emoji_slug %}
{% if node.icon %}  <span class="mdx-badge__icon">{{ icon }}</span>
{% endif %}
{% if node.get_text() %}  <span class="mdx-badge__text">{{ node.get_text() }}</span>
{% endif %}
</span>
"""

Code

mknodes.basenodes.mkmaterialbadge.MkMaterialBadge

class MkMaterialBadge(mktemplate.MkTemplate):
    """Node for a CSS-based badge a la MkDocs-Material."""

    ICON = "simple/shieldsdotio"
    CSS = [resources.CSSFile("materialbadge.css")]

    def __init__(
        self,
        icon: str,
        text: str = "",
        *,
        animated: bool = False,
        align_right: bool = False,
        target: linkprovider.LinkableType | None = None,
        **kwargs: Any,
    ) -> None:
        """Constructor.

        Args:
            icon: Icon to display. Can either be an iconify or an emoji slug
            text: Text to display
            animated: Optional animated style
            align_right: Right-align badge
            target: An optional URL / page target for the badge
            kwargs: Keyword arguments passed to parent
        """
        super().__init__("output/html/template", **kwargs)
        self.icon = icon
        self._text = text
        self.animated = animated
        self.align_right = align_right
        self.target = target

    def get_text(self) -> str:
        return self._text

Page info
Code for this page

mknodes.manual.nodes_section.create_section_for_nodes

def create_section_for_nodes(
    nav: mk.MkNav,
    klasses: Sequence[type[mk.MkNode]],
) -> mk.MkTable:
    table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
    for kls in klasses:
        tpl = "classpage_custom.jinja"
        page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
        nav += page
        if kls.STATUS:
            page.metadata.status = kls.STATUS
        elif kls.JS_FILES:
            page.metadata.status = "js"
        elif kls.CSS:
            page.metadata.status = "css"
        page.created_by = create_section_for_nodes
        link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
        extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
        table.add_row((link, kls.__doc__, extensions))
    return table

Resources
Resources(css=[],
          markdown_extensions={'admonition': {},
                               'pymdownx.details': {},
                               'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
                                                                           'format': <function fence_code_format at 0x7d707a397060>,
                                                                           'name': 'mermaid'}]},
                               'pymdownx.tabbed': {},
                               'tables': {}},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
  source_function: create_section_for_nodes
  source_line_no: 93
hide:
- toc
icon: simple/shieldsdotio
status: new
title: MkMaterialBadge